package com.example.demo.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.kevinsawicki.http.HttpRequest;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import io.swagger.models.auth.In;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Copyright © 合生活. All rights reserved.
 *
 * @Title: Examination
 * @Prject: ttc
 * @Package: com.feejson.ttc.util
 * @Description: 考试
 * @author: feejson
 * @date: 2019-06-21 13:28
 * @version: V1.0
 */
public class Examination {


//
//    http://edu.zxietong.com:81/xtedu/api/exercises/setExerciseResult //一个题目
//
//    userId	000000
//    exercisesId	402840a76b543509016b54c1ead80322
//    answer	0,1,2,3,4,5
//    trueanswers	0,1,2,3,4,5
//    type	3
//    fromType	3
//    isTrue	1
//    scaleId	ff8080816b5516e8016b56a57f150048
//
//    http://edu.zxietong.com:81/xtedu/api/exercises/setTestResult  //全部提交
//
//    scaleId	ff8080816b5516e8016b56a57f150048
//    userId	000000
//
//    http://edu.zxietong.com:81/xtedu/api/exercises/getPackExercise?time=new%20Date() // 获取考试卷
//
//    time	userId	000000
//    http://edu.zxietong.com:81/xtedu/api/exercises/getOnline // 获取题目
//
//
//    userId	000000
//    scaleId	ff8080816b5516e8016b56a57f150048
//    exercisesType	-1
//    fromType	3

    //获取考试卷
    public final  static  String GETTHETESTVOLUMEADDRESS = "http://edu.zxietong.com:81/xtedu/api/exercises/getPackExercise" ;
    //获取题目
    public final  static  String  GETTHETOPIC = "http://edu.zxietong.com:81/xtedu/api/exercises/getOnline" ;

//    "http://edu.zxietong.com:81/xtedu/api/exercises/getOnlineResult"

    //一个题目提交
    public final static  String ATOPIC = "http://edu.zxietong.com:81/xtedu/api/exercises/setExerciseResult";

    //全部提交
    public final static  String SUBMITALL = "http://edu.zxietong.com:81/xtedu/api/exercises/setTestResult";


    public static Random random = new Random();
    private static final Logger logger = LoggerFactory.getLogger(Examination.class);

    public static void main(String[] args) {
//        http://edu.zxietong.com:81/xtedu/api/exercises/getOnline?userId=563631&scaleId=ff80808171c041390171ca3d391f07c2&exercisesType=-1&fromType=3
        String url = "http://edu.zxietong.com:81/xtedu/api/exercises/getOnlineResult";
        String url2 = "http://edu.zxietong.com:81/xtedu/api/exercises/getOnline";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("scaleId","ff80808171c041390171ca3d391f07c2");
        jsonObject.put("userId","563631");
        jsonObject.put("exercisesType",-1);
        jsonObject.put("fromType",3);
        StringBuilder u = new StringBuilder(url);
        u.append("?userId=").append("563631").append("&scaleId=").append("ff80808171c041390171ca3d391f07c2").append("&exercisesType=-1").append("&fromType=3");
//        563631
        String body = HttpRequest.post(url).form(jsonObject).body();
        System.out.println(body);
//        下面是前台行为规范标准行为红线的是 ff80808171c041390171ca2260490320 ff80808171c041390171ca2260490320 ff80808171c041390171ca2260490320
    }

    public static void startTheExam (String userId) {
        //获取考试卷
        List<JSONObject> theTestPaper = getTheTestPaper(userId);
        for (int a = 0;a<theTestPaper.size();a++){
            JSONObject p = theTestPaper.get(a);
            String scaleId = p.getString("scaleId");
            getTheTitleUnderTheTestPaper(userId, scaleId);
            try {
//                int i = random.nextInt(3)+2;
                int i = 1;
                logger.info(userId+"试卷随机间隔时间"+i+"分钟");
//                Thread.sleep(i*1000*60);
                Thread.sleep(i*1000*10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        logger.info(userId+"考试完成！");
    }

    /**
     * 全部提交
     * @param scaleId
     * @param userId
     */
    public static  void submitAll(String scaleId,String userId){
        Map<String,Object> map = Maps.newHashMap();
        map.put("userId",userId);
        map.put("scaleId",scaleId);
        String examPaper = HttpRequest.post(SUBMITALL).form(map).body();
        System.out.println("全部提交结果   -- " + examPaper);
    }


    /**
     * 提交一个题目
     * @param userId
     * @param scaleId
     * @param trueanswers
     * @param exercisesId
     * @param answerParam
     */
    public static  void submitAQuestion(String userId ,String scaleId,String trueanswers,String exercisesId, String answerParam,Integer isTrue){
        Map<String,Object> map = Maps.newHashMap();
        map.put("userId",userId);
        map.put("trueanswers",trueanswers);
        map.put("exercisesId",exercisesId);
        map.put("answer",answerParam);
        map.put("scaleId",scaleId);
        map.put("userAnswers",trueanswers);
        map.put("isTrue",isTrue);
        map.put("fromType",3);
        map.put("type",2);
        String examPaper = HttpRequest.post(ATOPIC).form(map).body();
        System.out.println("回答了一个题目" + examPaper);
    }

    /**
     * 获取试卷下题目
     */
    private static  void getTheTitleUnderTheTestPaper(String userId,String scaleId) {
        int error = random.nextInt(15);
        int f = random.nextInt(2);
        Map map = new HashMap<>();
        map.put("userId",userId);
        map.put("scaleId",scaleId);
        map.put("exercisesType",-1);
        map.put("fromType",3);
        //获取全部考题
        String examPaper = HttpRequest.post(GETTHETOPIC).form(map).body();
        System.out.println(examPaper);
        //需要考试的全部考题
        JSONObject jsonObjects = JSON.parseObject(examPaper);
        Object o = jsonObjects.get("result");
        if (o instanceof JSONArray){
            JSONArray result = jsonObjects.getJSONArray("result");
            int size = result.size();
            for (int i = 0;i<size;i++){
                JSONObject bean  = (JSONObject)result.get(i);
                //取正确答案
                String trueanswersStr = bean.getString("right");
                String type = bean.getString("type");
                if ("4".equals(type)){
                    List<JSONObject>  answer = (List<JSONObject>) bean.get("answer");
                    JSONObject answerBean = answer.get(answer.size() - 1);
                    String title = answerBean.getString("title");
                    trueanswersStr = title;
                }
                String userAnswers = trueanswersStr;
                Integer isTrue = 1;
                /*if (size>15 && f == 1){
                    if (i == error){
                        isTrue = 0;
                        userAnswers = "0";
                        if (userAnswers.equals(trueanswersStr)){
                            userAnswers = "1";
                        }
                        if ("3".equals(type)){
                            userAnswers = "0,1";
                            if (userAnswers.equals(trueanswersStr)){
                                userAnswers = "0,1,2";
                            }
                        }
                    }
                }*/
                String exercisesId = bean.getString("exercisesId");
                submitAQuestion(userId,scaleId,trueanswersStr, exercisesId, userAnswers,isTrue);
                try {
                    int a = random.nextInt(2)+1;
                    logger.info(userId+"答题随机间隔时间"+a+"秒");
                    Thread.sleep(a*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            //结尾提交
            submitAll(scaleId,userId);
        }else {
            System.out.println("result ERROR:"+JSON.toJSONString(o));
        }
    }


    /**
     * 获取考试卷
     * @param emplyeeCode 工号
     */
    private static  List<JSONObject>  getTheTestPaper(String emplyeeCode) {
        Map map = new HashMap<>();
        map.put("userId",emplyeeCode);//"000000"
        map.put("time",System.currentTimeMillis());
        //考试卷
        String examPaper = HttpRequest.post(GETTHETESTVOLUMEADDRESS).form(map).body();
        System.out.println(examPaper);
        //需要考试的考试卷
        List<JSONObject> jsonObjects = paraseJSONAray(examPaper);
        return  jsonObjects;
    }

    public static  List<JSONObject> paraseJSONAray(String content){
        List<JSONObject> resultArr = Lists.newArrayList();
        JSONObject data = JSON.parseObject(content);
        JSONArray result = data.getJSONArray("result");
        for (Object p:result){
            JSONObject bean  = (JSONObject)p;
            Integer state = bean.getInteger("state");
            resultArr.add(bean);
        }
        return  resultArr;
    }

}
